<!--
 * @Author: your name
 * @Date: 2021-11-18 02:30:22
 * @LastEditTime: 2021-12-03 16:52:50
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \admin-template\src\views\task\edit.vue
-->
<template>
  <el-dialog :visible.sync="visible" :title="dataForm.id ? `修改` : `新增`" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" center width="35%" custom-class="dialogStyle treeDialog">
    <template slot="title">
      <div class="headerBack">
        <div class="iconBox">
          <div class="imgBox" />
        </div>
        <div v-if="!dataForm.id" class="titleBox">新增</div>
        <div v-else class="titleBox">修改</div>
      </div>
    </template>
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="150px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model.trim="dataForm.taskName" maxlength="32" />
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="dataForm.taskType" placeholder="选择任务类型">
          <el-option v-for="(item,index) in typeList" :key="index" :label="item.label" :value="item.id" />
        </el-select>
      </el-form-item>

      <div v-if="dataForm.taskType == 2">
        <!-- <el-form-item label="仿生附件" prop="simulationEnclosure">
          <el-upload class="upload-demo" action="" :accept="fileAccept" :http-request="upFileBySim" :file-list="fileList.file5" :limit="1">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item> -->

        <el-form-item label="TDR计算" prop="tdrCount">
          <el-select v-model="dataForm.tdrCount" placeholder="" clearable>
            <el-option v-for="(item,index) in calcList" :key="index" :label="item" :value="item" />
          </el-select>
        </el-form-item>

        <el-form-item label="DDR频域计算" prop="ddrFrequencyCount">
          <el-select v-model="dataForm.ddrFrequencyCount" placeholder="" clearable>
            <el-option v-for="(item,index) in calcList" :key="index" :label="item" :value="item" />
          </el-select>
        </el-form-item>

        <el-form-item label="DDR时域计算" prop="ddrTimeCount">
          <el-select v-model="dataForm.ddrTimeCount" placeholder="" clearable>
            <el-option v-for="(item,index) in calcList" :key="index" :label="item" :value="item" />
          </el-select>
        </el-form-item>

        
        
        <el-form-item label="仿真参数范围" prop="startIndex">
          <el-input-number v-model="dataForm.startIndex" :min="1" :max="100" label="描述文字" />
           - 
          <el-input-number v-model="dataForm.endIndex" :min="1" :max="100" label="描述文字" />
        </el-form-item>

        <el-form-item label="netGroup" prop="netGroups">
          <el-select v-model="dataForm.netGroups" value-key="id" style="width: 100%" multiple placeholder="netGroup" @clear="clearNetGroups" @change="changeNetGroups">
            <el-option v-for="(item) in netGroups" :key="item.id" :label="item.label" :value="item" />
          </el-select>
        </el-form-item>

        <el-form-item label="netGroupVal" v-if="dataForm.netGroups.length > 0" prop="netGroupVal">
          <div v-for="(item,index) in dataForm.netGroups" :key="index">
            {{ item.label }}：<el-input v-model.trim="dataForm.netGroupVals[item.label]" style="width: 90%;"  maxlength="32" placeholder="" />
          </div>
        </el-form-item>

        <el-form-item label="规范校核种类" prop="checkType">
          <el-select v-model="dataForm.checkType" style="width: 100%" placeholder="选择规范校核种类">
            <el-option v-for="(item,index) in checkTypeList" :key="index" :label="item.label" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="导入模型附件" prop="modelEnclosure">
          <el-upload class="upload-demo" action="" :accept="fileAccept" :http-request="upFileByModel" :file-list="fileList.file6" :limit="1">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </div>

      <div v-if="dataForm.taskType == 1">
        <el-form-item label="Abaqus导入模型" prop="file">
          <el-upload class="upload-demo" action="" :accept="fileAccept" :http-request="uploadFile1" :file-list="fileList.file1" :limit="1">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="Siwave导入模型" prop="file">
          <el-upload class="upload-demo" action="" :accept="fileAccept" :http-request="uploadFile2" :file-list="fileList.file2" :limit="1">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="Flothherm导入模型" prop="file">
          <el-upload class="upload-demo" action="" :accept="fileAccept" :http-request="uploadFile3" :file-list="fileList.file3" :limit="1">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="Cst导入模型" prop="file">
          <el-upload class="upload-demo" action="" :accept="fileAccept" :http-request="uploadFile4" :file-list="fileList.file4" :limit="1">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="变形优化指标" prop="shapeIndex">
          <el-input v-model.trim="dataForm.shapeIndex" maxlength="32" />
        </el-form-item>
        <el-form-item label="散热优化指标" prop="heatIndex">
          <el-input v-model.trim="dataForm.heatIndex" maxlength="32" />
        </el-form-item>
      </div>
    </el-form>
    <template slot="footer">
      <el-button type="primary" class="saveBtn" @click="dataFormSubmitHandle()">提交计算</el-button>
      <el-button @click="visible = false;clearForm('dataForm')">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import '@/assets/scss/dialogGlobal.scss'
import debounce from 'lodash/debounce'

import { businessTaskFile, businessTaskSimulation, businessTaskReport } from '@/api/businessTask/api'

export default {
  data() {
    return {
      typeList: [
        { id: 1, label: '多学科联合仿真及优化业务' },
        { id: 2, label: '基于ANSYS的DDR自动化仿真' }
      ],
      calcList: ['Siwave', 'EDT'],
      netGroups: [
        { id: 1, label: 'DDR_MA' },
        { id: 2, label: 'DDR_DQ' },
        { id: 3, label: 'DDR_DQS' },
        { id: 4, label: 'DDR_CLK' }
      ],
      checkTypeList: [
        { id: 1, label: 'DDR4' },
        { id: 2, label: 'LPDDR4' },
        { id: 3, label: 'DDR5' },
        { id: 4, label: 'LPDDR5' }
      ],
      fileAccept: '',
      fileList: {
        file1: [],
        file2: [],
        file3: [],
        file4: [],
        file5: [],
        file6: [],
      },
      visible: false,
      dataForm: {
        // 任务类型1
        taskName: '',
        taskType: '',
        shapeIndex: '',
        heatIndex: '',
        // 任务类型2
        tdrCount: '',
        ddrFrequencyCount: '',
        ddrTimeCount: '',
        startIndex: '',
        endIndex: '',
        netGroup: '',
        netGroups: [],
        netGroupVals: {
          DDR_MA: '',
          DDR_DQ: '',
          DDR_DQS: '',
          DDR_CLK: '',
        },
        checkType: '',
      },
      dataRule: {
        taskName: [
          { required: true, message: '请填写', trigger: 'change' }
        ],
        taskType: [
          { required: true, message: '请填写', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 初始化
    init() {
      this.visible = true
      if (this.dataForm.id) {
        // this.getInfo()
      }
    },
    // 上传文件
    uploadFile1(file) {
      const postData = new FormData()
      postData.append('file', file.file)
      this.upBusinessTaskFile(`enclosureTypeAbaqus`, postData)
    },
    uploadFile2(file) {
      const postData = new FormData()
      postData.append('file', file.file)
      this.upBusinessTaskFile(`enclosureTypeSiwave`, postData)
    },
    uploadFile3(file) {
      const postData = new FormData()
      postData.append('file', file.file)
      this.upBusinessTaskFile(`enclosureTypeFlothherm`, postData)
    },
    uploadFile4(file) {
      const postData = new FormData()
      postData.append('file', file.file)
      this.upBusinessTaskFile(`enclosureTypeCst`, postData)
    },
    upFileBySim(file) {
      const postData = new FormData()
      postData.append('file', file.file)
      this.upBusinessTaskFile(`simulationEnclosure`, postData)
    },
    upFileByModel(file) {
      const postData = new FormData()
      postData.append('file', file.file)
      this.upBusinessTaskFile(`modelEnclosure`, postData)
    },
    upBusinessTaskFile(str, postData) {
      businessTaskFile(postData).then((res) => {
        if (res.code != 200) { return this.$message.error(res.message); }
        this.dataForm[str] = res.data
        // console.log(this.dataForm)
      })
    },
    changeNetGroups(val) {
      // console.log(val)
    },
    clearNetGroups(val) {
      // console.log(val)
    },
    compareById(arg) {
      return function(a, b) {
        return a[arg] - b[arg];
      }
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function() {
      this.$refs.dataForm.validate((valid) => {
        if (!valid) {
          return false
        }
        
        if (this.dataForm.taskType == 1) {
          businessTaskSimulation(this.dataForm).then(res => {
            if (res.code == 200) {
              this.visible = false
              this.clearForm('dataForm')
              this.$emit('refreshDataList')
              this.$message({
                message: '操作成功！,正在计算中...',
                type: 'success',
                duration: 1500,
              })
              // this.$router.push({ path: `` })
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          const netGroup = this.dataForm.netGroups.sort(this.compareById('id'))
          const netGroupLabel = this.dataForm.netGroups.sort(this.compareById('label')).map((item) => { return item.label })
          // console.log(netGroupLabel)
          this.dataForm.netGroup = netGroup.map((item,index) => {
            return item.id
          }).join(',')
          
          const vals = this.dataForm.netGroupVals
          let valArr = []

          if(Object.keys(vals).length > 0) {
            for(let i in vals) {
              if(vals[i] && netGroupLabel.includes(i)) {
                valArr.push(vals[i])
              } else {
                valArr.push('')
              }
            }
          } else {
            const len = netGroup.length
            for(let i = 0; i < len; i++) {
              valArr.push('')
            }
          }
          this.dataForm.netGroupVal = valArr.join(',')
          // console.log('dataForm', this.dataForm, this.dataForm.netGroup, valArr, this.dataForm.netGroupVal)
          businessTaskReport(this.dataForm).then(res => {
            if (res.code == 200) {
              this.visible = false
              this.clearForm('dataForm')
              this.$emit('refreshDataList')
              // this.$message.success(`操作成功！,正在计算中...`)
              this.$message({
                message: '操作成功！,正在计算中...',
                type: 'success',
                duration: 1500,
              })
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    }),
    // 清空表单
    clearForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
